package org.zn.note.jdk.net;

import org.zn.note.common.util.IOUtil;
import org.zn.note.common.util.ThreadUtil;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ClassName Server
 * @Author zn
 * @Date 2019/4/4 14:40
 **/
public class BioServer {

    final static int PORT = 1016;

    public static void main(String[] args) throws IOException {
        new BioServer().server1();
    }

    private void server1() throws IOException {
        ThreadPoolHandler threadPoolHandler = new ThreadPoolHandler(50, 1000);
        ServerSocket serverSocket = null;
        try {
            // 不捕获这个IOException
            serverSocket = new ServerSocket(PORT);
            ThreadUtil.printThread("server start at port " + PORT);
            while (true) {
                try {
                    Socket socket = serverSocket.accept();
                    ThreadUtil.printThread("server connect " + socket.getRemoteSocketAddress());
                    threadPoolHandler.execute(new ServerHandler(socket));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            IOUtil.close(serverSocket);
        }
    }
}
